package com.financing.platform.web.api;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.BizEnums;
import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dao.impl.JpushMessageDaoImpl;
import com.financing.platform.dto.FpShopInformationDTO;
import com.financing.platform.dto.MessageJpushDTO;
import com.financing.platform.service.IFpShopInformationService;
import com.financing.platform.util.DTOdateSet;

@Controller("fpShopInformationController")
@RequestMapping("/api/fpShopInformation")
public class FpShopInformationController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private IFpShopInformationService fpShopInformationService;
	@Autowired
	JpushMessageDaoImpl jpushMessageDaoImpl;
	
	@Value("#{settings['immediateList_url']}")
	private  String immediateList_url;
	
	@Value("#{settings['hotList_url']}")
	private  String hotList_url;
	
	@Value("#{settings['moringList_url']}")
	private  String moringList_url;

	@RequestMapping("/listFpShopInformation.shtml")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, HttpServletResponse response, FpShopInformationDTO fpShopInformation,Pager pager){
		ResultMap result = new ResultMap();

		try{
			logger.info("query fpShopInformation list begin .......");

			fpShopInformation.setBegin(pager.getStartIndex());
			fpShopInformation.setPageSize(pager.getPageSize());

			int count = fpShopInformationService.queryListCount(fpShopInformation);

			pager.setTotalCount(count);

			List<FpShopInformationDTO> fpShopInformationList = null;

			if(count > 0){
				fpShopInformationList = fpShopInformationService.queryList(fpShopInformation);
			}else{
				fpShopInformationList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpShopInformationList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query fpShopInformation list end .......");
		}catch(Exception e){
			log.error("query fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return result;
	}

	@RequestMapping("/addFpShopInformation.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, HttpServletResponse response, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("add fpShopInformation  begin .......");

			DTOdateSet.setCreateInfo(fpShopInformation, request.getSession());
			DTOdateSet.setUpdateInfo(fpShopInformation, request.getSession());

			Date preTime = fpShopInformation.getPreTime();
			if(preTime == null) {
				fpShopInformation.setReleaseTime(new Date());
			} else {
				fpShopInformation.setReleaseTime(preTime);
			}

			int id = fpShopInformationService.insert(fpShopInformation);
			
			//消息推送
			if(StringUtils.equals(fpShopInformation.getPushMeg(), BizEnums.PushMegStatusEnums.AMOUNT_TYPE_BZJ)) {
				MessageJpushDTO messageJpushDTO = new MessageJpushDTO();
				
				messageJpushDTO.setFpJpushTitle(fpShopInformation.getTitle());
				messageJpushDTO.setFpJpushContent(fpShopInformation.getBirefDesc());
				JSONObject json = new JSONObject();
				json.put("id", fpShopInformation.getId());
				json.put("type", fpShopInformation.getType());
				messageJpushDTO.setFpJpushRedrict(json.toString());//跳转地址
				messageJpushDTO.setFpJpushMsgType("06");//消息类型 01:系统消息 02:运营推送 04:理财师工具身份认证审核 05 理财师工具预约通知 06:金策师资讯版块
				
				if(preTime!=null) {
					messageJpushDTO.setFpJpushSendType("02");// 推送消息类型  01:实时推送  02:定时推送
					messageJpushDTO.setFpJpushSendTime(fpShopInformation.getPreTime());//定时发送时间
				} else {
					messageJpushDTO.setFpJpushSendType("01");// 推送消息类型  01:实时推送  02:定时推送
				}
				
				messageJpushDTO.setCreateBy("");
				messageJpushDTO.setCreateDate(new Date());
				messageJpushDTO.setUpdateBy("");
				messageJpushDTO.setUpdateDate(new Date());
				messageJpushDTO.setFpJpushSource("shop"); //加店铺标识
				jpushMessageDaoImpl.addJpushMessage(messageJpushDTO);
			}
			
			result.setAttribute("id", id);

			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("add fpShopInformation list end .......");
		}catch(Exception e){
			log.error("add fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return result;
	}

	@RequestMapping("/editFpShopInformation.shtml")
	@ResponseBody
	public ResultMap edit(HttpServletRequest request, HttpServletResponse response, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("add fpShopInformation  begin .......");

			DTOdateSet.setCreateInfo(fpShopInformation, request.getSession());
			DTOdateSet.setUpdateInfo(fpShopInformation, request.getSession());

			Date preTime = fpShopInformation.getPreTime();
			if(preTime == null) {
				fpShopInformation.setReleaseTime(new Date());
			} else {
				fpShopInformation.setReleaseTime(preTime);
			}
			
			fpShopInformationService.update(fpShopInformation);
			
			//消息推送
			if(StringUtils.equals(fpShopInformation.getPushMeg(), BizEnums.PushMegStatusEnums.AMOUNT_TYPE_BZJ)) {
				MessageJpushDTO messageJpushDTO = new MessageJpushDTO();
				
				messageJpushDTO.setFpJpushTitle(fpShopInformation.getTitle());
				messageJpushDTO.setFpJpushContent(fpShopInformation.getBirefDesc());
				JSONObject json = new JSONObject();
				json.put("id", fpShopInformation.getId());
				json.put("type", fpShopInformation.getType());
				messageJpushDTO.setFpJpushRedrict(json.toString());//跳转地址
				messageJpushDTO.setFpJpushMsgType("06");//消息类型 01:系统消息 02:运营推送 04:理财师工具身份认证审核 05 理财师工具预约通知 06:金策师资讯版块
				
				if(preTime!=null) {
					messageJpushDTO.setFpJpushSendType("02");// 推送消息类型  01:实时推送  02:定时推送
					messageJpushDTO.setFpJpushSendTime(fpShopInformation.getPreTime());//定时发送时间
				} else {
					messageJpushDTO.setFpJpushSendType("01");// 推送消息类型  01:实时推送  02:定时推送
				}
				
				messageJpushDTO.setCreateBy("");
				messageJpushDTO.setCreateDate(new Date());
				messageJpushDTO.setUpdateBy("");
				messageJpushDTO.setUpdateDate(new Date());
				messageJpushDTO.setFpJpushSource("shop"); //加店铺标识
				jpushMessageDaoImpl.addJpushMessage(messageJpushDTO);
			}

			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("add fpShopInformation list end .......");
		}catch(Exception e){
			log.error("add fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return result;
	}


	@RequestMapping("/detailFpShopInformation.shtml")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, HttpServletResponse response, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("query detail fpShopInformation  begin .......");

			Integer id = fpShopInformation.getId();

			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			FpShopInformationDTO dto = fpShopInformationService.loadById(id);

			result.setAttribute("dto",dto);

			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query detail fpShopInformation  end .......");
		}catch(Exception e){
			log.error("query detail fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return result;
	}

	@RequestMapping("/deleteFpShopInformation.shtml")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, HttpServletResponse response, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("delete fpShopInformation  begin .......");

			Integer id = fpShopInformation.getId();

			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			fpShopInformationService.delete(id);

			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("delete fpShopInformation  end .......");
		}catch(Exception e){
			log.error("delete fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return result;
	}

	@RequestMapping("/updateFpShopInformation.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, HttpServletResponse response, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("update fpShopInformation  begin .......");

			Integer id = fpShopInformation.getId();

			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			//			DTOdateSetUtil.setUpdateInfo(fpShopInformation);
			DTOdateSet.setUpdateInfo(fpShopInformation, request.getSession());

			fpShopInformationService.update(fpShopInformation);

			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("update fpShopInformation list end .......");
		}catch(Exception e){
			log.error("update fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return result;
	}

	@RequestMapping("/updateForceFpShopInformation.shtml")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, HttpServletResponse response, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("updateForce fpShopInformation  begin .......");

			Integer id = fpShopInformation.getId();

			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			//			DTOdateSetUtil.setUpdateInfo(fpShopInformation);

			fpShopInformationService.updateForce(fpShopInformation);


			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("updateForce fpShopInformation list end .......");
		}catch(Exception e){
			log.error("updateForce fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return result;
	}

	/**
	 * 资讯上下架
	 * 
	 * @param request
	 * @param response
	 * @param fpShopInformation
	 * @return
	 */
	@RequestMapping("/upordownFpShopInformation.shtml")
	@ResponseBody
	public ResultMap upordownFpShopInformation(HttpServletRequest request, HttpServletResponse response, FpShopInformationDTO fpShopInformation){
		ResultMap result = new ResultMap();

		try{
			logger.info("add fpShopInformation  begin .......");

			DTOdateSet.setCreateInfo(fpShopInformation, request.getSession());
			DTOdateSet.setUpdateInfo(fpShopInformation, request.getSession());

			fpShopInformationService.update(fpShopInformation);

			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("add fpShopInformation list end .......");
		}catch(Exception e){
			log.error("add fpShopInformation error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return result;
	}
}
